package jmsTopic;

import java.io.IOException;
import java.io.PrintWriter;

import javax.jms.JMSException;
import javax.jms.Session;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicSession;
import javax.jms.TopicSubscriber;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class MessageConsumerServlet
 */
public class MessageConsumerAServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	private TopicConnectionFactory conFactory;
	private TopicConnection connection;
	private TopicSession session;
	private TopicSubscriber subscriber;
	private Topic topic;
	private Listener listener;
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MessageConsumerAServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		HttpSession httpSession = request.getSession();
		
		receiveMessage();
		
		OneMessage oneMsg = (OneMessage)httpSession.getAttribute("message");

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		
		out.println("<html><body>");
		out.println("<br/>");
		out.println("<form method=\"GET\" action=\"homePage.jsp\" >");
		out.println("<h3>We have a new register, with name : " + oneMsg.getFirstname() + "." + oneMsg.getLastname() + "<br/>" + " and login is : " + oneMsg.getLogin() + "</h3>");
		out.println("<h4>you can check this in the log file </h4>");
		out.println("<br/>");
		out.println("<input type=\"submit\" value=\"Return\" />");
		out.println("</form>");
		out.println("</body></html>");
		
		httpSession.removeAttribute("message");
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	
	protected void receiveMessage() {
		// JNDI cherche ConnectionFactory 
		listener = new Listener();
		
		try {
			InitialContext init = new InitialContext();
			conFactory = (TopicConnectionFactory) init.lookup("java:comp/env/jms/ConnectionFactory");
			topic = (Topic) init.lookup("java:comp/env/jms/topic/MyTopic");
		} catch (NamingException e) {
			System.exit(1);
		}
		
		try {
			connection = conFactory.createTopicConnection();
			connection.setClientID("Liying");
			session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
			
			subscriber = session.createDurableSubscriber(topic, "ConsomerA");	
			subscriber.setMessageListener(listener);

			// demarrer la JMS connection 
			connection.start();
			
		} catch (JMSException e) {
			e.printStackTrace();
		}
				
	}
	

}
